﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Auction
{
    class Validations
    {
         ErrorProvider errorProvider1=new ErrorProvider();

        public void LimpiarErrores()
        {
            errorProvider1.Clear();
        }

        public bool ValidarControlNumerico(TextBox unControl)
        {
            Decimal result=-1;
            bool valorValido = true;
            if (Decimal.TryParse(unControl.Text, out result) == false)
            {
                errorProvider1.SetError(unControl, "Escriba un valor numérico.");
                return false;
            }
            if (result < 0)
            {
                errorProvider1.SetError(unControl, "Escriba un valor positivo.");
                return false;
            }
            return valorValido;
        }

        public decimal GetDecimal(TextBox unControl)
        {
            Decimal result = -1;
            if (ValidarControlNumerico(unControl))
            {
                Decimal.TryParse(unControl.Text, out result);
            }
            return result;
        }

        public byte GetByte(TextBox unControl)
        {
            byte result = 0;
            if (ValidarControlNumerico(unControl))
            {
                byte.TryParse(unControl.Text, out result);
            }
            return result;
        }


        public int GetInt(TextBox unControl)
        {
            int result = 0;
            if (ValidarControlNumerico(unControl))
            {
                int.TryParse(unControl.Text, out result);
            }
            return result;
        }

        public int toInt(String st)
        {
            int returned = 0;
            int.TryParse(st, out returned);
            return returned;
        }

        public double Redondear(double numero,int numDecimales,bool haciaArriba)
        {
            //double decide = 0;
            //if (numDecimales > 0)
            //{
            //    decide = 1 / Math.Pow(10, (numDecimales + 1));
            //}
            //if(haciaArriba)
            //{
            //    numero=numero+decide;
            //}
            //else
            //{
            //    numero=numero-decide;                
            //}

            return Math.Round(numero, numDecimales);
        }

        public byte ObtenerByte(TextBox unControl)
        {
            Byte result = 0;
            if (ValidarControlNumerico(unControl))
            {
                Byte.TryParse(unControl.Text, out result);
            }
            return result;
        }

        public DateTime ObtenerDateTime(string fecha)
        {
            DateTime result = new DateTime();
            if (DateTime.TryParse(fecha, out result) == false)
            {
                MessageBox.Show("Error en el formato del Dia");
            }
            return result;
        }
    
    }
}
